﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;
using System.Text.RegularExpressions;




namespace SL4_NAV_ProjectiCON
{
    public partial class Operation : UserControl
    {

        private Random _random = new Random();
        public Operation()
        {           
            InitializeComponent();
            //MyMap.Center = new Location(40.71990, -73.99);
            //MyMap.ZoomLevel = 11;
            //On_Load(); 
          
        }

        public void Load()
        {
            //Clear();
            SL4_NAV_ProjectiCON.DataService.DataServiceClient objCust = new  SL4_NAV_ProjectiCON.DataService.DataServiceClient();
            objCust.GetCustomersCompleted += new EventHandler< SL4_NAV_ProjectiCON.DataService.GetCustomersCompletedEventArgs>(GetCustomersCompleted);
            objCust.GetCustomersAsync();
           
        }

        public void ReLoad(String objCuststr)
        {
           // Clear();
             SL4_NAV_ProjectiCON.DataService.DataServiceClient objCust1 = new  SL4_NAV_ProjectiCON.DataService.DataServiceClient();
            objCust1.GetCustomersBynameCompleted += new EventHandler< SL4_NAV_ProjectiCON.DataService.GetCustomersBynameCompletedEventArgs>(GetCustomersByNameCompleted);
            objCust1.GetCustomersBynameAsync(objCuststr);
        }
              
        void GetCustomersCompleted(object sender,  SL4_NAV_ProjectiCON.DataService.GetCustomersCompletedEventArgs e)
        {
            //MapLayer PushPinLayer = null;
            //PushPinLayer = new MapLayer();
            //PushPinLayer.Name = "PushPinLayer";
            //MyMap.Children.Add(PushPinLayer);
            //dataGrid1.ItemsSource = e.Result;  
            foreach ( SL4_NAV_ProjectiCON.DataService.Customer C in e.Result)
            {
                CustomerPushpin pin;
                Location d = new Location(Convert.ToDouble(C.Latitude), Convert.ToDouble(C.Longitude));
                if (C.CustomerId > 10050)
                    pin = new CustomerPushpin(Colors.Blue );
                else
                    pin = new CustomerPushpin(Colors.Red);

                pin.Location = d;
                pin.Name = C.CustomerId.ToString() ;
                pin.CustomerId = C.CustomerId.ToString();
                pin.CustomerName = C.CustomerName.ToString();
                ScaleTransform st = new ScaleTransform();
                st.ScaleX = 0.25;
                st.ScaleY = 0.25;
                st.CenterX = (pin as FrameworkElement).Height / 2;
                st.CenterY = (pin as FrameworkElement).Height / 2;

                pin.RenderTransform = st;
                //pin.MouseEnter += new MouseEventHandler(point_MouseEnter);
                //pin.MouseLeave += new MouseEventHandler(point_MouseLeave);
                //PushPinLayer.AddChild(pin, d);
            }
        }

        void GetCustomersByNameCompleted(object sender,  SL4_NAV_ProjectiCON.DataService.GetCustomersBynameCompletedEventArgs e)
        {
             //dataGrid1.ItemsSource = e.Result;
            
        }

        //public void Clear()
        //{
        //    List<UIElement> elementsToRemove = new List<UIElement>();
        //    List<UIElement> pushpinToRemove = new List<UIElement>();
        //    foreach (UIElement element in MyMap.Children)
        //    {
        //        if (element.GetType() == typeof(MapLayer))
        //        {
        //            MapLayer Lay = (MapLayer)element;
        //            if (Lay.Name == "PushPinLayer")
        //            {
        //                foreach (UIElement p in Lay.Children)
        //                {
        //                    if (p.GetType() == typeof(CustomerPushpin))
        //                    {
        //                        pushpinToRemove.Add(p);
        //                    }
        //                }
        //                foreach (UIElement pin in pushpinToRemove)
        //                {
        //                    Lay.Children.Remove(pin);
        //                }
        //            }
        //            elementsToRemove.Add(Lay);
        //        }
        //    }
        //    //foreach (UIElement e in elementsToRemove)
        //    //{
        //    //    MyMap.Children.Remove(e);
        //    //}
        //}

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            //Clear();
        }

        //void point_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        //{
        //    //Show a popup with data about that point
        //    CustomerPushpin pin = sender as CustomerPushpin;
        //    if (pin != null)
        //    {
        //        MapLayer.SetPosition(ContentPopup, pin.Location);
        //        MapLayer.SetPositionOffset(ContentPopup, new Point(0, -60));
               
                

        //        string contentString = pin.CustomerId.ToString() + "   " + pin.CustomerName.ToString();  
        //        //Remove tags from the string
        //        Regex regex = new Regex("<(.|\n)*?>");
        //        contentString = regex.Replace(contentString, string.Empty);
        //        ContentPopupText.Text = contentString;
        //        ContentPopup.Visibility = Visibility.Visible;

        //        ReLoad(pin.CustomerName.ToString());
                               
                 
        //    }
        //}

        //void point_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        //{
        //    //Hide the popup
        //    CustomerPushpin point = sender as CustomerPushpin;
        //    ContentPopup.Visibility = Visibility.Collapsed;
        //}

        private void ContentPopup_MouseEnter(object sender, MouseEventArgs e)
        {
            //Show the popup
            (sender as Canvas).Visibility = Visibility.Visible;
            Canvas.SetZIndex((sender as Canvas), 10);
        }

        private void ContentPopup_MouseLeave(object sender, MouseEventArgs e)
        {
            //Show the popup
            (sender as Canvas).Visibility = Visibility.Collapsed;
        }

         //ICON SAMPLE CODE STARTS - 15-FEB-2011

        //private void On_Load()
        //{
        //    output.Text = "Button container clicked!";
        //    this.TR2N.Content = "TR2N";

        //    if (this.TR2N.Content.ToString() == "TR2N")
        //    {
        //        Border border = new Border();
        //        border.Width = 60;
        //        border.Height = 60;
        //        border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
        //        TR2N.Content = border;
        //        //TR2N.Content= "PANEL, PATCH,";
        //        output.Text = "PANEL - DAMAGE : PATCH , REPAIR- REPLACE";
        //    }

        //    if (this.RB4N.Content.ToString() == "RB4N")
        //    {
        //        Border border = new Border();
        //        border.Width = 60;
        //        border.Height = 60;
        //        border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
        //        RB4N.Content = border;
        //        //TR2N.Content= "PANEL, PATCH,";
        //        output.Text = "PANEL - DAMAGE : DENT , REPAIR- STRAIGHTEN";
        //    }

        //    if (this.RH5N.Content.ToString() == "RH5N")
        //    {
        //        Border border = new Border();
        //        border.Width = 60;
        //        border.Height = 60;
        //        border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
        //        RH5N.Content = border;
        //        //TR2N.Content= "PANEL, PATCH,";
        //        output.Text = "CROSS MEMBER - DAMAGE : PATCH , REPAIR- REPLACE";
        //    }

        //    if (this.UX3N.Content.ToString() == "UX3N")
        //    {
        //        Border border = new Border();
        //        border.Width = 60;
        //        border.Height = 60;
        //        border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
        //        UX3N.Content = border;
        //        //TR2N.Content= "PANEL, PATCH,";
        //        output.Text = "UNDERSTRUCTURE PANEL- DAMAGE : SCRATCHES, REPAIR- WASHING & CLEANING";
        //    }

        //    if (this.DB2N.Content.ToString() == "DB2N")
        //    {
        //        Border border = new Border();
        //        border.Width = 60;
        //        border.Height = 60;
        //        border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
        //        DB2N.Content = border;
        //        //TR2N.Content= "PANEL, PATCH,";
        //        output.Text = "DOOR PANEL - DAMAGE : DENT , REPAIR- STRAIGHTEN";
        //    }
        //}

        private void OnButtonTransformInteractionControlClick(object sender, EventArgs e)
        {
            ////output.Text = "Button container clicked!";
            //this.TR2N.Content = "TR2N";

            //if (this.TR2N.Content.ToString() == "TR2N")
            //{
            //    Border border = new Border();
            //    border.Width = 60;
            //    border.Height = 60;
            //    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
            //    TR2N.Content = border;
            //    output.Text = "PANEL - DAMAGE : PATCH , REPAIR- REPLACE";
            //}
        }

        private void OnBorderTransformInteractionControlClick(object sender, EventArgs e)
        {
            //output.Text = "Border container clicked!";

            //if (this.RB4N.Content.ToString() == "RB4N")
            //{
            //    Border border = new Border();
            //    border.Width = 60;
            //    border.Height = 60;
            //    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
            //    RB4N.Content = border;
            //    //TR2N.Content= "PANEL, PATCH,";
            //    output.Text = "PANEL - DAMAGE : DENT , REPAIR- STRAIGHTEN";
            //}
            //if (this.UX3N.Content.ToString() == "UX3N")
            //{
            //    Border border = new Border();
            //    border.Width = 60;
            //    border.Height = 60;
            //    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
            //    UX3N.Content = border;
            //    //TR2N.Content= "PANEL, PATCH,";
            //    output.Text = "UNDERSTRUCTURE PANEL- DAMAGE : SCRATCHES, REPAIR- WASHING & CLEANING";
            //}
        }

        private void OnButtonTransformInteractionControlClick_Test2(object sender, EventArgs e)
        {
            //output.Text = "Test 2- Button container clicked!";

            //if (this.RH5N.Content.ToString() == "RH5N")
            //{
            //    Border border = new Border();
            //    border.Width = 60;
            //    border.Height = 60;
            //    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
            //    RH5N.Content = border;
            //    //TR2N.Content= "PANEL, PATCH,";
            //    output.Text = "CROSS MEMBER - DAMAGE : PATCH , REPAIR- REPLACE";
            //}

            //if (this.DB2N.Content.ToString() == "DB2N")
            //{
            //    Border border = new Border();
            //    border.Width = 60;
            //    border.Height = 60;
            //    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
            //    DB2N.Content = border;
            //    //TR2N.Content= "PANEL, PATCH,";
            //    output.Text = "DOOR PANEL - DAMAGE : DENT , REPAIR- STRAIGHTEN";
            //}
        }

        private void OnButtonTransformInteractionControlClick_Text(object sender, EventArgs e)
        {
            //if (this.RH5N.Content.ToString() == "RH5N")
            //{
            //    Border border = new Border();
            //    border.Width = 60;
            //    border.Height = 60;
            //    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
            //    RH5N.Content = border;
            //    //TR2N.Content= "PANEL, PATCH,";
            //    output.Text = "CROSS MEMBER - DAMAGE : PATCH , REPAIR- REPLACE";
            //}

            //if (this.DB2N.Content.ToString() == "DB2N")
            //{
            //    Border border = new Border();
            //    border.Width = 60;
            //    border.Height = 60;
            //    border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
            //    DB2N.Content = border;
            //    //TR2N.Content= "PANEL, PATCH,";
            //    output.Text = "DOOR PANEL - DAMAGE : DENT , REPAIR- STRAIGHTEN";
            //}
        }

        private void OnAddButtonClick(object sender, RoutedEventArgs e)
        {
            TransformInteraction.TransformInteractionControl transformControl = new TransformInteraction.TransformInteractionControl();
            transformControl.HorizontalAlignment = HorizontalAlignment.Center;
            transformControl.VerticalAlignment = VerticalAlignment.Center;
            Border border = new Border();
            border.Width = 50;
            border.Height = 50;
            border.Background = new SolidColorBrush(Color.FromArgb(255, (byte)_random.Next(255), (byte)_random.Next(255), (byte)_random.Next(255)));
            transformControl.Content = border;
            LayoutRoot.Children.Add(transformControl);
        }

    }


    public class CustomerPushpin : Pushpin
    {
        public string CustomerName { get; set; }
        public string CustomerId { get; set; }
        public string CustomerAddress { get; set; }
        public CustomerPushpin(Color Bg)
        {
            this.Name = Guid.NewGuid().ToString();
            SolidColorBrush scb = new SolidColorBrush(Bg);
            this.Background = scb;
        }
    }
}
